package com.drinks.yiniang.common.recycler.decoration;

import android.graphics.Rect;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.drinks.yiniang.App;
import com.hansen.library.utils.ScreenSizeUtils;

/**
 * 只有第一个item有top
 * Created by han on 2019/6/20 0020.
 */

public class FirstItemDecoration extends RecyclerView.ItemDecoration {
    private int mSpace;

    private boolean hasTop;
    private boolean hasBottom;

    public FirstItemDecoration(int space) {
        this.mSpace = space;
        this.hasTop = true;
        this.hasBottom = false;
    }

    public static FirstItemDecoration getItemDPDecoration(int space) {
        return new FirstItemDecoration(ScreenSizeUtils.dp2px(App.getInstance().getApplicationContext(), space));
    }

    public static FirstItemDecoration getItemPXDecoration(int space) {
        return new FirstItemDecoration(space);
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {

        // Add top margin only for the first item to avoid double space between items
        if (parent.getChildLayoutPosition(view) == 0) {
            if(hasTop) {
                outRect.top = mSpace;
            }
            if(hasBottom) {
                outRect.bottom = mSpace;
            }
        } else {
            outRect.top = 0;
            outRect.bottom = 0;
        }
    }
}
